package summary;

import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/18 19:14
 */
public class Title32 {

    public int longestValidParentheses(String s) {
        Stack<Integer> stack = new Stack<>();
        stack.push(-1);
        int max = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                //下标入栈
                stack.push(i);
            }else{
                //当前为右括号，栈顶弹出
                stack.pop();
                if (stack.isEmpty()) {
                    //放入当前')'的下标
                    stack.push(i);
                }
                max = Math.max(max, i - stack.peek());
            }
        }
        return max;
    }

    public static void main(String[] args) {

    }
}
